<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="600px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="农场名称">
        <el-input v-model="farmInfo.farmName" disabled />
      </el-form-item>
      <el-form-item label="农场编号">
        <el-input v-model="farmInfo.farmNo" disabled />
      </el-form-item>
      <el-form-item label="违规时间" prop="violationTime">
        <el-date-picker
          v-model="formData.violationTime"
          type="datetime"
          placeholder="请选择违规时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="违规原因" prop="violationReason">
        <el-input
          v-model="formData.violationReason"
          type="textarea"
          :rows="5"
          placeholder="请输入违规原因"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ViolationRecordApi, ViolationRecordVO } from '@/api/farm/violationrecord'

/** 农场违规记录 表单 */
defineOptions({ name: 'ViolationRecordForm' })

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('添加违规记录') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中
const farmInfo = ref({
  farmId: undefined as number | undefined,
  farmName: '',
  farmNo: ''
})

const formData = ref({
  farmId: undefined as number | undefined,
  farmNo: '',
  farmName: '',
  violationTime: undefined,
  violationReason: ''
})

const formRules = reactive({
  violationTime: [{ required: true, message: '违规时间不能为空', trigger: 'change' }],
  violationReason: [
    { required: true, message: '违规原因不能为空', trigger: 'blur' },
    { min: 2, max: 500, message: '违规原因长度应在 2 到 500 个字符之间', trigger: 'blur' }
  ]
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (farmId: number, farmName: string, farmNo: string) => {
  dialogVisible.value = true
  resetForm()
  
  // 设置农场信息（用于显示）
  farmInfo.value = {
    farmId: farmId,
    farmName: farmName,
    farmNo: farmNo
  }
  
  // 设置表单数据（用于提交）
  formData.value.farmId = farmId
  formData.value.farmNo = farmNo
  formData.value.farmName = farmName
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = { ...formData.value } as unknown as ViolationRecordVO
    await ViolationRecordApi.createViolationRecord(data)
    message.success('添加违规记录成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    farmId: undefined as number | undefined,
    farmNo: '',
    farmName: '',
    violationTime: undefined,
    violationReason: ''
  }
  formRef.value?.resetFields()
}
</script>

